/** 使用须知
 *   获取表格数据方法名统一为 getData
 *   数据集合命名为 data
 *   提交表单命名为 searchForm  参数 page 默认值1
 *   getData获取数据成功 必须return { total }  total数据总条数
 *   接口参数 req_param 从外到内填写接口名称
 *
 * 接口结构示例 外层文件夹名称 > 不同js文件分类 > 接口方法名称,按顺序填写相应名称
 * urls:{
   0:'schoolConfig',
   1:'classCircleList',
   2:'handleDelete'
}

 实际调取过程 this.$api['schoolConfig']['classCircleList']['getPageData']()
 **/
export default {
    data() {
        return {
            isFirst: true,
            reqNum: 0, //请求次数
            // 表格分页参数
            page: {
                currentPage: 1,
                total: 0, //总条数
                layout: "total, sizes, prev, pager, next, jumper",
                pageSize: 15, //当前显示条数
                pageSizes: [5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300], //分页每一页条数
            },

            loading: true, //表格加载变量

            // 页面绑定表格的对象用 searchForm
            searchForm: {
                page: 1, //查询页码
                limit: 15, //查询条数
            },
            // 页面获取表格数据的接口
            urls: {
                0: "schoolConfig",
                1: "classCircleList",
            },
        };
    },
    methods: {
        // 搜索事件
        async handleSearchChange(params, done) {
            this.loading = true;
            this.searchForm.page = 1;
            this.page.currentPage = 1;
            //  将schoolName转为schoolid
            if (this.searchForm.schoolName && !isNaN(this.searchForm.schoolName)) this.searchForm.schoolId = this.searchForm.schoolName;
            //自定义搜索回调
            if (this.searchForm.date && this.searchForm.date.length) {
                this.searchForm.startTime = this.searchForm.date[0]
                this.searchForm.endTime = this.searchForm.date[1]
                delete this.searchForm.date
            }
            this.searchCallBack ? await this.searchCallBack(Object.assign({}, this.searchForm)) : await this.getData(this.searchForm);
            this.loading = false;
            done();
        },
        // // 清空回调
        handleSearchReset() {
            this.searchForm.page = 1
            this.searchForm.limit = 15
            this.searchForm.schoolId = this.$store.state.team.currTeam.id
            console.log("this.searchForm", this.searchForm)
        },

        // 分页加载
        async onLoad(page) {
            // console.log("page", page);
            this.searchForm.page = this.page.currentPage;
            this.searchForm.limit = this.page.pageSize;
            await this.getData(this.searchForm);
        },
        /**
         *生成数据 调取api
         * @param currentPage 查询的页码
         * @param pageSize 每次查询的条数
         * @returns {Promise<void>}
         */
        async getData(param, isPage = false) {
            //处理提交数据
            let subForm = Object.assign({}, param);
            delete subForm.schoolName;
            delete subForm.$schoolName;

            this.loading = true;
            //调取api
            let api = this.$api;
            Object.keys(this.urls).forEach((key) => {
                api = api[this.urls[key]];
                // console.log("api",api)
            });

            // 赋值
            const res = await api(subForm);

            if (isPage) {
                this.loading = false;
                return res.data.records;
            }

            const second = this.reqNum++ ? 0 : 500;
            //首次延迟赋值 解决表单分页被盖住
            setTimeout(async () => {
                // this.dataCallBack 处理数据回调
                this.dataCallBack ? await this.dataCallBack(res.data) : (this.data = (res.data && res.data.records) || res.data);
                this.page.total = (res.data && res.data.total) || 1; //设置总条数  渲染实现页码
                this.loading = false;
            }, second);


        },
    },
};
